
library(tidyverse)
library(rio)
library(cregg)
library(patchwork)

# set replication folder as working directory
setwd("~replication")

load("data_genderedcost_long.rdata")

# only include completed answers - and answers given before deadline
# 2021-12-20 21:49:52 was the last response within the time frame
df_long <- df_long %>% 
  filter(SurveyStatus==2)

df_long <- df_long %>% 
  filter(SurveyEndTime<="2021-12-20 21:49:52")

### FIGURE 9
## Marginal means across experienced and inexperienced candidates

df_1 <- df_long %>% 
  dplyr::filter(electoral_performance=="First-time elected" |
                  electoral_performance=="Not elected, not elected previously") 

df_2 <- df_long %>% 
  dplyr::filter(electoral_performance=="Re-elected" |
                  electoral_performance=="Not elected, but elected previously") 

mm_1 <- cregg::mm(df_1, # data
                  choice~position + remuneration + workload + work_environment, # formula
                  id = id)

mm_1$subset <- "Inexperienced" # add label


mm_2<- cregg::mm(df_2, # data
                 choice~position + remuneration + workload + work_environment, # formula
                 id = id)

mm_2$subset <- "Experienced" # add label

mm_experience <- bind_rows(mm_1, mm_2) %>% 
  mutate(feature = case_when(feature=="work_environment"~"Working Environment",
                             feature=="workload"~"Workload",
                             feature=="remuneration"~"Remuneration",
                             feature=="position"~"Position")) %>% 
  mutate(feature = factor(feature, levels = c("Position", "Remuneration", "Workload", "Working Environment"))) %>%
  mutate(subset = factor(subset, levels = c("Experienced",
                                            "Inexperienced")))

## Subgroup marginal means
## Left hand-side of figure 9
mm_experience_plot <- mm_experience %>% 
  ggplot(data=., aes(y = level, x = estimate, color = subset, shape = subset)) +
  geom_point(position = position_dodge2(width = 1)) +
  geom_linerange(aes(xmin=estimate-(std.error*1.39),
                     xmax=estimate+(std.error*1.39)),
                 position = position_dodge2(width = 1)) +
  xlab("Marginal mean") +
  ylab("") +
  scale_color_manual("", values = c("black", "gray", "black", "gray")) +
  scale_shape_manual("", values=c(16,17,1,2)) + #(16,17,1,2)
  scale_linetype_manual("", values = c(2,2,1,1)) +
  scale_x_continuous(breaks = seq(0.2,0.8,0.05), labels = seq(0.2,0.8,0.05)) +
  geom_vline(xintercept = 0.5, linetype = "dashed") +
  theme_bw() +
  facet_wrap(~feature, scales = "free_y", ncol = 1) +
  theme(legend.position = "top", panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0, face = "bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  guides(color=guide_legend(ncol=2, byrow=TRUE))


## estimates for the difference in marginal means
df_long <- df_long %>%
  mutate(experience = factor(ifelse(electoral_performance=="First-time elected" |
                                      electoral_performance=="Not elected, not elected previously","No experience","Experience")))

mm_exp_dif <- cregg::mm_diffs(df_long, # data
                              choice~position + remuneration + workload + work_environment, # formula
                              ~experience)

mm_exp_dif <- mm_exp_dif %>%
  mutate(feature = case_when(feature=="work_environment"~"Working Environment",
                             feature=="workload"~"Workload",
                             feature=="remuneration"~"Remuneration",
                             feature=="position"~"Position")) %>% 
  mutate(feature = factor(feature, levels = c("Position", "Remuneration", "Workload", "Working Environment")))

mm_experience_dif_plot <- mm_exp_dif %>% 
  ggplot(data=., aes(y = level, x = estimate)) + #shape = subset, linetype = subset)) +
  geom_point(color = "black", position = position_dodge2(width = 0.8)) +
  geom_linerange(aes(xmin=lower,
                     xmax=upper),
                 color = "black", position = position_dodge2(width = 0.8)) +
  xlab("Differences\n(inexperienced - experienced)") +
  ylab("") +
  scale_x_continuous(labels = seq(-0.2,0.2,0.05), breaks = seq(-0.2,0.2,0.05)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  theme_bw() +
  scale_linetype_manual("", values = c(2,1)) +
  scale_shape_manual("", values = c(1,16)) +
  facet_wrap(~feature, scales = "free_y", ncol = 1) +
  coord_cartesian(xlim = c(-0.12,0.12)) +
  theme(legend.position = "none", panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0, face = "bold"),
        axis.text.y=element_blank(), axis.ticks.y=element_blank(),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  guides(color=guide_legend(ncol=1, byrow = TRUE, reverse = TRUE),
         shape=guide_legend(ncol=1, byrow = TRUE, reverse = TRUE),
         linetype=guide_legend(ncol=1, byrow = TRUE, reverse = TRUE)) 

mm_experience_plot + mm_experience_dif_plot + plot_layout(widths = c(2, 1))

ggsave("figure9.pdf", height = 5, width = 10)
ggsave("figure9.png", height = 5, width = 10, dpi = 600)
